<?php
require_once 'modules/bookmark/const.inc';
class Helper_Bookmark extends Zend_Controller_Action_Helper_Abstract
{
    private $_ctrl;
    private $_prefix;
    
    public function init()
    {
        $this->_ctrl = $this->getActionController();    
        $this->_prefix = BOOKMARK_CACHE_PREFIX;        
    }
                                                                                    
    public function getUserBookmarks($uid, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {   
        $bookmarks = array();
        $key = 'home_user_bookmarks_' . $uid;       
        if ($page == 1){
            $bookmarks = $this->_ctrl->getCache($key, $this->_prefix);
            if ( ! $bookmarks){
                $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');                
                $bookmarks = $bookmarkModel->getUserBookmarks($uid, $page, $perPage);        
                $this->_ctrl->setCache($key, $bookmarks, $this->_prefix);
            }
        } else {
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');                
            $bookmarks = $bookmarkModel->getUserBookmarks($uid, $page, $perPage);         
        }   
        return $bookmarks;
    }  
    
    public function getUserBookmarkCount($uid)
    {                                                           
        $key = 'user_bookmark_count_' . $uid;
        $count = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $count){
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
            $count = $bookmarkModel->getUserBookmarkCount($uid);
            $this->_ctrl->setCache($key, $count, $this->_prefix);
        }
        return $count;
    }      
    
    private function _getBookmarksByType($ref, $type, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {
        if ($page == 1){
            $key = 'bookmarks_type_' . $type . '_' . $ref;
            $bookmarks = $this->_ctrl->getCache($key, $this->_prefix);
            if ( ! $bookmarks){
                $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
                $bookmarks = $bookmarkModel->getBookmarksByType($ref, $type, $page, $perPage);    
                $this->_ctrl->setCache($key, $bookmarks, $this->_prefix);
            }
        } else {
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
            $bookmarks = $bookmarkModel->getBookmarksByType($ref, $type, $page, $perPage);        
        } 
        return $bookmarks;       
    }                 
    
    public function getGroupBookmarks($gid, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {
        return $this->_getBookmarksByType($gid, BOOKMARK_TYPE_GROUP, $page, $perPage);
    }
    
    public function getEventBookmarks($eid, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {
        return $this->_getBookmarksByType($eid, BOOKMARK_TYPE_EVENT, $page, $perPage);
    }    
    
    public function getNetworkBookmarks($nid, $page = 1, $perPage = BOOKMARKS_PER_PAGE)
    {           
        //$key = 'network_bookmark_' . $nid;    
        //$bookmarks = $this->_ctrl->getCache($key, $this->_prefix);
        //if ( ! $bookmarks){
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
            $networkHelper = $this->_ctrl->getHelper('Network');    
            $uids = $networkHelper->getNetworkUID($nid);
            $bookmarks = $bookmarkModel->getUsersBookmarks($uids, $page, $perPage);
            //$this->_ctrl->setCache($key, $bookmarks, $this->_prefix, 21600);
        //}                                        
        return $bookmarks;
    }      
    
    public function getNetworkBookmarkCount($nid)
    {           
        $key = 'network_bookmark_count_' . $nid;        
        $count = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $count){
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
            $networkHelper = $this->_ctrl->getHelper('Network');    
            $uids = $networkHelper->getNetworkUID($nid);
            $count = $bookmarkModel->getUsersBookmarkCount($uids);
            $this->_ctrl->setCache($key, $count, $this->_prefix, 21600);
        }
        return $count;
    }
    
    private function _getBookmarkCountByType($id, $type)
    {
        $key = 'bookmark_count_' . $type . '_' . $id;
        $count = $this->_ctrl->getCache($key, $this->_prefix);
        if ( ! $count){
            $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
            $count = $bookmarkModel->countBookmarkByType($id, $type);    
            $this->_ctrl->setCache($key, $count, $this->_prefix);
        }
        return $count;
    }
    
    public function getGroupBookmarkCount($gid)
    {
        return $this->_getBookmarkCountByType($gid, BOOKMARK_TYPE_GROUP); 
    }
    
    public function getEventBookmarkCount($eid)
    {
        return $this->_getBookmarkCountByType($eid, BOOKMARK_TYPE_EVENT);     
    }   
    
    private function _deleteBookmark($bid, $ref, $type)
    {
        if ($type != 0 AND $ref != 0){ 
            if ($type == BOOKMARK_TYPE_GROUP){
                $helperName = 'Groups';
            } elseif ($type == BOOKMARK_TYPE_EVENT) {
                $helperName = 'Event';  
            } else {
                exit();
            }
            $user = $this->_ctrl->getUser();
            $helper = $this->_ctrl->getHelper($helperName);
            if ( ! $helper->isCanRemoveBookmark($ref, $user->uid)){
                exit();
            }
        }
        $bookmarkModel = AppGlobal::loadModel('Bookmark', 'bookmark');
        $bookmarkModel->deleteBookmark($bid, $ref, $type);  
        $key = 'bookmarks_type_' . $type . '_' . $ref;
        $this->_ctrl->deleteCache($key, $this->_prefix);      
        $key = 'bookmark_count_' . $type . '_' . $ref;
        $this->_ctrl->deleteCache($key, $this->_prefix);
    }
    
    public function deleteEventBookmark($bid, $eid)
    {
        $this->_deleteBookmark($bid, $eid, BOOKMARK_TYPE_EVENT);
    }
    
    public function deleteGroupBookmark($bid, $gid)
    {
        $this->_deleteBookmark($bid, $gid, BOOKMARK_TYPE_GROUP);
    }
    
    private function _getBookmark($bid)
    {
        $key = 'bookmark_info_' . $bid ;
        $bookmark = $this->getCache($key, $this->_prefix);
        if ( ! $bookmark) {
            $bookmarkModel = $this->loadModel('Bookmark', 'bookmark');
            $bookmark = $bookmarkModel->getBookmark($bid);
            $this->setCache($key, $bookmark, $this->_prefix);
        }
        return $bookmark;
    }
}

